package com.shijie.service;

import java.util.List;

import com.shijie.domain.entity.Users;
import com.shijie.mapper.SignsMapper;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.entity.Signs;
import com.shijie.domain.enums.EnumInterface.IsUsed;

import javax.annotation.Resource;

import com.shijie.mapper.UsersMapper;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.shijie.component.AppUserService;
import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@Service
public class SignsService extends AbstractService<Signs> {

	@Resource
	private SignsMapper signsMapper;
	@Resource
	private MagazinesService magazinesService;
	@Resource
	private AppUserService appUserService;
	@Resource
	private UsersMapper usersMapper;

	public List<Signs> selectSignsByActivityId(Integer activityId){
		Example condition  = new Example(Signs.class);
		condition.createCriteria().andEqualTo("activityId", activityId);
        condition.orderBy("registrationDate").desc();
		return  signsMapper.selectByCondition(condition);
	}


	public List<Signs> selectSignsByUserId(Integer userId) {
		Example condition = new Example(Signs.class);
		condition.createCriteria().andEqualTo("userId", userId);
		condition.orderBy("registrationDate").desc();
		return signsMapper.selectByCondition(condition);
	}

    public boolean isSign(Integer activityId, Integer userId) {
        Example condition = new Example(Signs.class);
        condition.createCriteria().andEqualTo("activityId", activityId).andEqualTo("userId", userId);
        condition.orderBy("registrationDate").desc();
        List<Signs> list = signsMapper.selectByCondition(condition);
        return list.size() > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    public void sign( Signs signs) throws ServiceException {
		Magazines magazines = magazinesService.selectById(signs.getActivityId());
		if (magazines == null) throw new ServiceException("报名活动不存在");
		List<Signs> total = this.selectAll();
		if (total.size() >= magazines.getLimits()) {
			throw new ServiceException("报名人数已够！");
		}
		if (DateTime.now().isBefore(magazines.getStartTime().getTime())) {
			throw new ServiceException("活动未开始");
		}
		if (DateTime.now().isAfter(magazines.getEndTime().getTime())) {
			throw new ServiceException("活动已结束");
		}
		if (this.isSign(magazines.getId(), appUserService.getCurrentUser().getId())) {
			throw new ServiceException("已经报过名");
		}
		Users user = usersMapper.detailForUser(appUserService.getCurrentUser().getId());
		signs.setUserId(user.getId());
		signs.setUserName(appUserService.getUserName());
		signs.setCreateBy(appUserService.getUserName());
		signs.setCreateTime(DateTime.now().toDate());
		signs.setRegistrationDate(DateTime.now().toDate());
		signs.setIsUsed(IsUsed.NOUSED.ordinal());
		signs.setAvatar(appUserService.getCurrentUser().getAvatar());
		signs.setPhoneNo(user.getPhoneNo());
		this.save(signs);
	}
}
